package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.AnswerComment;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.AnswerQueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("answers")
public class AnswerController {
    @Autowired
    private IAnswerService answerService;

    @Autowired
    private IQuestionService questionService;
    /**
     * 根据问题的id列出所有的回答
     * @param qo
     * @return
     */
    @GetMapping("/{questionId}/list")
    public Object  list(Model model, @ModelAttribute("qo") AnswerQueryObject qo){
        qo.setOrderBy("a.createTime desc");
        PageInfo list = answerService.listAll(qo);
        return list;
    }

    /**
     * 保存答案
     * @param answer
     * @param questionId
     * @return
     */
    @PostMapping
    public JsonResult save(Answer answer,Long questionId){
        JsonResult result = new JsonResult();
        try {
            User currentUser = UserContext.getCurrentUser();
            //设置作者id
            answer.setUser(currentUser);
            //设置问题的id
            Question question = questionService.getById(questionId);
            answer.setQuestion(question);
            answerService.save(answer);

        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 保存当前答案的评论
     * @param answerId
     * @param content
     * @return
     */
    @PostMapping("/comment")
    public JsonResult saveComment(@RequestParam Long answerId, @RequestParam String content){
        JsonResult jsonResult = new JsonResult();

        AnswerComment answerComment = new AnswerComment();
        //设置用户
        User currentUser = UserContext.getCurrentUser();
        answerComment.setUser(currentUser);
        //设置答案
        Answer answer = answerService.getById(answerId);
        answerComment.setAnswer(answer);
        //设置状态
        answerComment.setState(AnswerComment.UNREAD);
        //设置评论时间
        answerComment.setCreateTime(new Date());
        //设置评论内容
        answerComment.setContent(content);

        answerService.saveByAnswerId(answerComment);

        Answer answer1nswer = answerService.getById(answerId);
        Integer commentCount = answer1nswer.getCommentCount();
        if (commentCount==null){
            commentCount=0;
        }
        //答案评论数加一
        answerService.updateCommentCount(commentCount+1,answerId);

        return jsonResult;
    }

    /**
     * 根据当前答案的id查询评论
     * @param answerId
     * @return
     */
    @GetMapping("/comment/{answerId}")
    public JsonResult getCommentByAnswerId(@PathVariable Long answerId){
        JsonResult jsonResult = new JsonResult();

        List<AnswerComment> list = answerService.getCommentByAnswerId(answerId);
        return jsonResult.setResult(list);
    }
}
